package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.Dao.Impl.NewsClassIficationDaoImpl;
import com.ysd.Dao.Impl.ProductSeriesDaoImpl;
import com.ysd.entity.NewsClassiIfication;
import com.ysd.entity.ProductSeries;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;




public class AdminProductCategoryServlet extends BaseServlet {
	/**
	 * 分页查询所有产品分类
	 * localhost:8080/zhubao/adminProductCategory?reqName=getProductCates
	 * @author 86176
	 *
	 */
	public void getProductCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		ProductSeriesDaoImpl psDao= new ProductSeriesDaoImpl();//创建实现类对象
		List<ProductSeries> psList=psDao.getProductCategoryByPage(page, size);//查询所有产品分类集合
		
		response.setCharacterEncoding("UTF-8");//设置响应编码格式
		PrintWriter out = response.getWriter();
		
		Gson gson= new Gson();
		String res = gson.toJson(psList);//将psList转化为json字符串
		
		out.print(res);//响应数据
		
		
		out.flush();
		out.close();
	}
	/**
	 * 获取产品分类
	 *  localhost:8080/zhubao/adminProductCategory?reqName=getAllProductCate
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
		public void getAllProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ProductSeriesDaoImpl psDao= new ProductSeriesDaoImpl();//创建实现类对象
			List<ProductSeries> psList=psDao.getProductSeriesList();//查询所有产品分类集合
			
			response.setCharacterEncoding("UTF-8");//设置响应编码格式
			PrintWriter out = response.getWriter();
			
			Gson gson= new Gson();
			String res = gson.toJson(psList);//将psList转化为json字符串
			
			out.print(res);//响应数据
			
			
			out.flush();
			out.close();
		}
	
/**
 * 获取产品分类的集合长度
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
	public void getAllProductCates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductSeriesDaoImpl psDao= new ProductSeriesDaoImpl();//创建实现类对象
		List<ProductSeries> psList=psDao.getProductSeriesList();//查询所有产品分类集合
		
		response.setCharacterEncoding("UTF-8");//设置响应编码格式
		PrintWriter out = response.getWriter();
		
		Gson gson= new Gson();
		String res = gson.toJson(psList.size());//将psList转化为json字符串
		
		out.print(res);//响应数据
		
		
		out.flush();
		out.close();
	}
	/**
	 * 删除指定产品分类
	 * localhost:8080/zhubao/adminProductCategory?reqName=deleteProductCate
	 * @author 86176
	 *
	 */
		public void deleteProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int pId = Integer.parseInt(request.getParameter("productSeriesId"));
			ProductSeriesDaoImpl psDao= new ProductSeriesDaoImpl();//创建实现类对象
			int n = psDao.deleteProductCategoryById(pId);//调用删除方法
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			if (n>0) {//删除成功
				out.print(Result.toClient(true, "删除成功",""));//{"success":true,"message":"删除成功","remark":""}	
			} else {//删除失败
				out.print(Result.toClient(false, "删除失败",""));//{"success":false,"message":"删除失败","remark":""}	
			}
			
			out.flush();
			out.close();
		}
		
		/**
		 *批量 删除指定产品分类
		 * localhost:8080/zhubao/adminProductCategory?reqName=deleteProductCategoryByIds
		 * @author 86176
		 *
		 */
			public void deleteProductCategoryByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				
				String pIds = request.getParameter("productSeriesId");
				ProductSeriesDaoImpl psDao= new ProductSeriesDaoImpl();//创建实现类对象
				int n = psDao.deleteProductCategoryByIds(pIds);//调用删除方法
				if (n>0) {//删除成功
					out.print(Result.toClient(true, "删除成功",""));//{"success":true,"message":"删除成功","remark":""}	
				} else {//删除失败
					out.print(Result.toClient(false, "删除失败",""));//{"success":false,"message":"删除失败","remark":""}	
				}
				
				out.flush();
				out.close();
			}
		/**
		 * 添加指定产品分类
		 * localhost:8080/zhubao/adminProductCategory?reqName=addProductCate
		 * @author 86176
		 *
		 */
		public void addProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			ProductSeriesDaoImpl psDao= new ProductSeriesDaoImpl();//创建实现类对象
			
			String productSeriesName =request.getParameter("productSeriesName");
			System.out.println(productSeriesName);
			if(productSeriesName != null && !productSeriesName.equals("")) {
				ProductSeries pcList =  psDao.getProductSeriesByName(productSeriesName);
				if(pcList == null) {
					ProductSeries ps = new ProductSeries(0, productSeriesName);
					int n =psDao.addProductCategory(ps);//调用添加产品类别方法
					
					if (n>0) {//添加成功
						out.print(Result.toClient(true, "添加成功",""));//{"success":true,"message":"删除成功","remark":""}	
					} else {//添加失败
						out.print(Result.toClient(false, "添加失败",""));//{"success":false,"message":"删除失败","remark":""}	
					}
					
				}else {
					out.print(Result.toClient(false, "添加失败,系列已存在",""));
				}
			}
			
			out.flush();
			out.close();
		}
		/**
		   *根据Id查询指定产品分类
		 * localhost:8080/zhubao/adminProductCategory?reqName=getProductCateById
		 * @author 86176
		 *
		 */
		public void getProductCateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			ProductSeriesDaoImpl psDao= new ProductSeriesDaoImpl();//创建实现类对象
			
			int productSeriesId =Integer.parseInt(request.getParameter("pSId"));//获取前台传来的参数             
			ProductSeries psList = psDao.getProductSeriesList(productSeriesId);//调用根据id查询的方法
			
			Gson gson= new Gson();
			String res = gson.toJson(psList);//将psList转化为json字符串
			
			out.print(res);//响应数据
			System.out.println(res);
			
					
			out.flush();
			out.close();
		}
		/**
		 * 编辑指定产品分类
		 * localhost:8080/zhubao/adminProductCategory?reqName=editProductCate
		 * @author 86176
		 *
		 */
		public void editProductCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String productSeriesName =request.getParameter("productSeriesName");
			int productSeriesId =Integer.parseInt(request.getParameter("productSeriesId"));
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			ProductSeriesDaoImpl psDao= new ProductSeriesDaoImpl();
			
				ProductSeries pcList =  psDao.getProductSeriesByName(productSeriesName);
				if(pcList == null) {
					ProductSeries ps = new ProductSeries(productSeriesId, productSeriesName);
					int n =psDao.editProductCategory(ps);
					
					if (n>0) {
						out.print(Result.toClient(true, "修改成功",""));
					} else {
						out.print(Result.toClient(false, "修改失败",""));	
					}
					
				}else {
					out.print(Result.toClient(false, "修改失败,系列已存在",""));
				}
			
			out.flush();
			out.close();
		}
}
